package haste

/*
#include "keydir.h"
*/
import (
		"C"
)

const MaxBucketSize = (1 << 20)

type KeyDir struct {
	ckd *C.keydir_t
}

func NewKeyDir(size uint32) *KeyDir {
	if size == 0 || size > MaxBucketSize {
		return nil
	}

	kd := new(KeyDir)
	kd.ckd = C.init_keydir(C.uint32_t(size))
	return kd
}

func (kd *KeyDir) destory() {
	C.destroy_keydir(kd.ckd)
}

func (kd *KeyDir) put(id uint64, offLen uint64) bool {
	ret := C.put_keydir(kd.ckd, C.uint64_t(id), C.uint64_t(offLen))
	return ret == 0
}

func (kd *KeyDir) get(id uint64) uint64 {
	offLen := C.get_keydir(kd.ckd, C.uint64_t(id))
	return uint64(offLen)
}
